import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;

public class ExamConfirmDialog extends JDialog {
    private Exam exam;
    private User student;
    private JFrame parent;
    private JButton confirmButton;
    private JButton cancelButton;

    public ExamConfirmDialog(JFrame parent, Exam exam, User student) {
        super(parent, "考试确认", true);
        this.parent = parent;
        this.exam = exam;
        this.student = student;
        initComponents();
        setupLayout();
        setupEvents();
    }

    private void initComponents() {
        setSize(650, 550);
        setLocationRelativeTo(parent);
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        setResizable(false);

        // 按钮设置
        Dimension buttonSize = new Dimension(130, 50);
        Font buttonFont = new Font("微软雅黑", Font.BOLD, 18);

        confirmButton = new JButton("确认开考");
        confirmButton.setPreferredSize(buttonSize);
        confirmButton.setFont(buttonFont);
        confirmButton.setBackground(new Color(40, 167, 69));
        confirmButton.setForeground(Color.WHITE);
        confirmButton.setBorder(BorderFactory.createEmptyBorder());
        confirmButton.setFocusPainted(false);

        cancelButton = new JButton("取消");
        cancelButton.setPreferredSize(buttonSize);
        cancelButton.setFont(buttonFont);
        cancelButton.setBackground(new Color(108, 117, 125));
        cancelButton.setForeground(Color.WHITE);
        cancelButton.setBorder(BorderFactory.createEmptyBorder());
        cancelButton.setFocusPainted(false);
    }

    private void setupLayout() {
        setLayout(new BorderLayout(0, 0));

        // 顶部标题面板
        JPanel titlePanel = new JPanel(new BorderLayout());
        titlePanel.setBackground(new Color(70, 130, 180));
        titlePanel.setPreferredSize(new Dimension(650, 80));

        JLabel titleLabel = new JLabel("考试确认", JLabel.CENTER);
        titleLabel.setFont(new Font("微软雅黑", Font.BOLD, 28));
        titleLabel.setForeground(Color.WHITE);

        JLabel subtitleLabel = new JLabel("请仔细阅读考试信息，确认后开始考试", JLabel.CENTER);
        subtitleLabel.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        subtitleLabel.setForeground(new Color(220, 220, 220));

        JPanel titleContent = new JPanel(new BorderLayout());
        titleContent.setBackground(new Color(70, 130, 180));
        titleContent.add(titleLabel, BorderLayout.CENTER);
        titleContent.add(subtitleLabel, BorderLayout.SOUTH);

        titlePanel.add(titleContent, BorderLayout.CENTER);

        // 主要内容面板
        JPanel mainPanel = new JPanel(new BorderLayout(20, 20));
        mainPanel.setBackground(Color.WHITE);
        mainPanel.setBorder(BorderFactory.createEmptyBorder(30, 30, 30, 30));

        // 考试信息面板
        JPanel infoPanel = createInfoPanel();

        // 注意事项面板
        JPanel noticePanel = createNoticePanel();

        mainPanel.add(infoPanel, BorderLayout.CENTER);
        mainPanel.add(noticePanel, BorderLayout.SOUTH);

        // 底部按钮面板
        JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 30, 25));
        buttonPanel.setBackground(Color.WHITE);
        buttonPanel.add(confirmButton);
        buttonPanel.add(cancelButton);

        add(titlePanel, BorderLayout.NORTH);
        add(mainPanel, BorderLayout.CENTER);
        add(buttonPanel, BorderLayout.SOUTH);
    }

    private JPanel createInfoPanel() {
        JPanel panel = new JPanel(new GridBagLayout());
        panel.setBackground(Color.WHITE);
        panel.setBorder(BorderFactory.createTitledBorder(
                BorderFactory.createLineBorder(new Color(70, 130, 180), 2),
                "考试信息",
                javax.swing.border.TitledBorder.LEFT,
                javax.swing.border.TitledBorder.TOP,
                new Font("微软雅黑", Font.BOLD, 18),
                new Color(70, 130, 180)));

        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(12, 15, 12, 15);
        gbc.anchor = GridBagConstraints.WEST;

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm");
        Font labelFont = new Font("微软雅黑", Font.BOLD, 16);
        Font valueFont = new Font("微软雅黑", Font.PLAIN, 16);

        // 考试名称
        gbc.gridx = 0;
        gbc.gridy = 0;
        JLabel nameLabel = new JLabel("考试名称：");
        nameLabel.setFont(labelFont);
        nameLabel.setForeground(new Color(70, 130, 180));
        panel.add(nameLabel, gbc);

        gbc.gridx = 1;
        JLabel nameValue = new JLabel(exam.getTitle());
        nameValue.setFont(valueFont);
        panel.add(nameValue, gbc);

        // 考试描述
        gbc.gridx = 0;
        gbc.gridy = 1;
        JLabel descLabel = new JLabel("考试描述：");
        descLabel.setFont(labelFont);
        descLabel.setForeground(new Color(70, 130, 180));
        panel.add(descLabel, gbc);

        gbc.gridx = 1;
        JTextArea descArea = new JTextArea(exam.getDescription());
        descArea.setFont(valueFont);
        descArea.setEditable(false);
        descArea.setBackground(Color.WHITE);
        descArea.setLineWrap(true);
        descArea.setWrapStyleWord(true);
        descArea.setRows(2);
        descArea.setColumns(25);
        panel.add(descArea, gbc);

        // 开始时间
        gbc.gridx = 0;
        gbc.gridy = 2;
        JLabel startLabel = new JLabel("开始时间：");
        startLabel.setFont(labelFont);
        startLabel.setForeground(new Color(70, 130, 180));
        panel.add(startLabel, gbc);

        gbc.gridx = 1;
        JLabel startValue = new JLabel(sdf.format(exam.getStartTime()));
        startValue.setFont(valueFont);
        panel.add(startValue, gbc);

        // 结束时间
        gbc.gridx = 0;
        gbc.gridy = 3;
        JLabel endLabel = new JLabel("结束时间：");
        endLabel.setFont(labelFont);
        endLabel.setForeground(new Color(70, 130, 180));
        panel.add(endLabel, gbc);

        gbc.gridx = 1;
        JLabel endValue = new JLabel(sdf.format(exam.getEndTime()));
        endValue.setFont(valueFont);
        panel.add(endValue, gbc);

        // 考试时长和总分
        gbc.gridx = 0;
        gbc.gridy = 4;
        JLabel durationLabel = new JLabel("考试时长：");
        durationLabel.setFont(labelFont);
        durationLabel.setForeground(new Color(70, 130, 180));
        panel.add(durationLabel, gbc);

        gbc.gridx = 1;
        JLabel durationValue = new JLabel(exam.getDuration() + " 分钟");
        durationValue.setFont(valueFont);
        panel.add(durationValue, gbc);

        gbc.gridx = 0;
        gbc.gridy = 5;
        JLabel scoreLabel = new JLabel("考试总分：");
        scoreLabel.setFont(labelFont);
        scoreLabel.setForeground(new Color(70, 130, 180));
        panel.add(scoreLabel, gbc);

        gbc.gridx = 1;
        JLabel scoreValue = new JLabel(String.valueOf(exam.getTotalScore()) + " 分");
        scoreValue.setFont(valueFont);
        panel.add(scoreValue, gbc);

        return panel;
    }

    private JPanel createNoticePanel() {
        JPanel panel = new JPanel(new BorderLayout());
        panel.setBackground(Color.WHITE);
        panel.setBorder(BorderFactory.createTitledBorder(
                BorderFactory.createLineBorder(new Color(220, 53, 69), 2),
                "重要提醒",
                javax.swing.border.TitledBorder.LEFT,
                javax.swing.border.TitledBorder.TOP,
                new Font("微软雅黑", Font.BOLD, 16),
                new Color(220, 53, 69)));

        String noticeText = "<html>" +
                "<div style='font-family: 微软雅黑; font-size: 14px; line-height: 1.6;'>" +
                "<b style='color: #dc3545;'>请仔细阅读以下注意事项：</b><br><br>" +
                "• <b>考试开始后不能暂停或重新开始</b><br>" +
                "• 请确保网络连接稳定，避免断网影响考试<br>" +
                "• 考试时间到后系统将自动提交试卷<br>" +
                "• 请认真作答，试卷提交后不能修改<br>" +
                "• 考试过程中请勿关闭浏览器或刷新页面<br>" +
                "• 如遇技术问题请及时联系监考老师" +
                "</div>" +
                "</html>";

        JLabel noticeLabel = new JLabel(noticeText);
        noticeLabel.setBorder(BorderFactory.createEmptyBorder(15, 20, 15, 20));

        panel.add(noticeLabel, BorderLayout.CENTER);
        return panel;
    }

    private void setupEvents() {
        confirmButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                startExam();
            }
        });

        cancelButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                dispose();
            }
        });

        // 添加按钮悬停效果
        confirmButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                confirmButton.setBackground(new Color(34, 139, 58));
            }

            public void mouseExited(java.awt.event.MouseEvent evt) {
                confirmButton.setBackground(new Color(40, 167, 69));
            }
        });

        cancelButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                cancelButton.setBackground(new Color(90, 98, 104));
            }

            public void mouseExited(java.awt.event.MouseEvent evt) {
                cancelButton.setBackground(new Color(108, 117, 125));
            }
        });
    }

    private void startExam() {
        // 检查考试时间
        long now = System.currentTimeMillis();
        if (now < exam.getStartTime().getTime()) {
            JOptionPane.showMessageDialog(this,
                    "考试还未开始！\n请等待考试开始时间。",
                    "提示",
                    JOptionPane.WARNING_MESSAGE);
            return;
        }

        if (now > exam.getEndTime().getTime()) {
            JOptionPane.showMessageDialog(this,
                    "考试已经结束！\n无法再参加此次考试。",
                    "提示",
                    JOptionPane.WARNING_MESSAGE);
            return;
        }

        // 最终确认
        int result = JOptionPane.showConfirmDialog(this,
                "确定要开始考试吗？\n考试开始后将无法取消！",
                "最终确认",
                JOptionPane.YES_NO_OPTION,
                JOptionPane.QUESTION_MESSAGE);

        if (result != JOptionPane.YES_OPTION) {
            return;
        }

        // 创建考试记录
        ExamRecordDAO recordDAO = new ExamRecordDAO();
        if (recordDAO.startExam(exam.getId(), student.getId())) {
            // 打开考试界面
            new ExamFrame(exam, student).setVisible(true);
            dispose();
            parent.dispose();
        } else {
            JOptionPane.showMessageDialog(this,
                    "开始考试失败！\n请联系管理员或稍后重试。",
                    "错误",
                    JOptionPane.ERROR_MESSAGE);
        }
    }
}